package com.gz.mylife.app;

import java.util.ArrayList;
import java.util.List;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.adapter.BaseAdapterHelper;
import com.gz.mylife.adapter.QuickAdapter;
import com.gz.mylife.widget.FooterView;
import com.gz.mylife.widget.FooterView.OnFooterViewRefreshListener;
import com.gz.mylife.widget.LoadingCover;
import com.gz.mylife.widget.OfflineDialog;
import com.gz.mylife.widget.LoadingCover.OnLoadingCoverRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zl.smartmall.library.IGZAddress;
import com.zl.smartmall.library.INotice;
import com.zl.smartmall.library.listener.GetNoticesListener;
import com.zl.smartmall.library.listener.RequestCode;
import com.zl.smartmall.library.po.ActivitiesLatestNoticeInfo;
import com.zl.smartmall.library.util.Tools;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class Notices extends BaseActivity implements OnLoadingCoverRefreshListener, OnFooterViewRefreshListener, OnRefreshListener<ListView>, OnItemClickListener {

	private int mPage;
	private QuickAdapter<ActivitiesLatestNoticeInfo> mAdapter;
	private FooterView mFooterView;
	private LoadingCover mLoadingCover;
	private OfflineDialog mOfflineDialog;
	private PullToRefreshListView mListView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notices);
		mAdapter = buildAdapter();

//		mActionBar.setTitle("物业公告");
		setActionbarTitle("物业公告", false);
		mFooterView = (FooterView) getLayoutInflater().inflate(R.layout.footer_view, null);
		mFooterView.setOnFooterViewRefreshListener(this);
		mLoadingCover = (LoadingCover) findViewById(R.id.layout_loading_cover);
		mLoadingCover.setOnLoadingCoverRefreshListener(this);
		mListView = (PullToRefreshListView) findViewById(R.id.list);
		mListView.getRefreshableView().addFooterView(mFooterView);
		mListView.setAdapter(mAdapter);
		mListView.setOnRefreshListener(this);
		mListView.setOnItemClickListener(this);
		mListView.setOnScrollListener(mFooterView);
		mOfflineDialog = (OfflineDialog) getLayoutInflater().inflate(R.layout.offline_dialog, null);
		
		onLoadingCoverRefresh();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.notices, menu);
		return true;
	}

	@Override
	public void onFooterViewRefresh() {
		// TODO Auto-generated method stub
		mPage++;
		INotice.getInstance().getNotices(IGZAddress.getInstance().getCurrentCommunityID(), mPage, buildListener(RequestCode.SCROLL));
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		ActivitiesLatestNoticeInfo activitiesLatestNoticeInfo = mAdapter.getItem((int) id);
		Intent intent = new Intent(getApplicationContext(), Browser.class);
		intent.putExtra("url", activitiesLatestNoticeInfo.getUrl());
		intent.putExtra("title", "最新公告");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}

	@Override
	public void onLoadingCoverRefresh() {
		// TODO Auto-generated method stub
		mPage++;
		INotice.getInstance().getNotices(IGZAddress.getInstance().getCurrentCommunityID(), mPage, buildListener(RequestCode.PULL));
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		mPage = 1;
		INotice.getInstance().getNotices(IGZAddress.getInstance().getCurrentCommunityID(), mPage, buildListener(RequestCode.PULL));
	}
	
	private QuickAdapter<ActivitiesLatestNoticeInfo> buildAdapter() {
		return new QuickAdapter<ActivitiesLatestNoticeInfo>(getApplicationContext(), R.layout.notices_item, new ArrayList<ActivitiesLatestNoticeInfo>()) {

			@Override
			protected void convert(BaseAdapterHelper helper, ActivitiesLatestNoticeInfo item) {
				helper.setText(R.id.tv_date, Tools.formatTimeStampString(getApplicationContext(), item.getTime(), false))
					  .setText(R.id.tv_title, item.getTitle());
				if(item.getIsTop() == 1) {
					helper.setImageResource(R.id.iv_flag, R.drawable.ic_flag_top)
						  .setVisible(R.id.iv_flag, View.VISIBLE);
				}else {
					helper.setImageResource(R.id.iv_flag, R.color.transparent)
					  .setVisible(R.id.iv_flag, View.GONE);
				}

				View divider = helper.getView(R.id.divider);
				if(helper.getPosition() == this.getCount() - 1) {
					((RelativeLayout.LayoutParams) divider.getLayoutParams()).leftMargin = 0;
					((RelativeLayout.LayoutParams) divider.getLayoutParams()).rightMargin = 0;
				}else {
					((RelativeLayout.LayoutParams) divider.getLayoutParams()).leftMargin = Tools.dip2px(getApplicationContext(), 15);
					((RelativeLayout.LayoutParams) divider.getLayoutParams()).rightMargin = Tools.dip2px(getApplicationContext(), 15);
				}
	
			}
		};
	}
	
	private GetNoticesListener buildListener(RequestCode requestCode) {
		return new GetNoticesListener(requestCode) {
			
			@Override
			public void onResponseFailed() {
				// TODO Auto-generated method stub
				mLoadingCover.onRefreshFailed();
				if(mListView.isRefreshing()) {
					mListView.getLoadingLayoutProxy().refreshFailed();
				}
				mPage--;
			}
			
			@Override
			public void onOffline() {
				// TODO Auto-generated method stub
				mOfflineDialog.show();
				mLoadingCover.onRefreshFailed(Define.MSG_OFFLINE_ERROR);
				mPage--;
			}
			
			@Override
			public void onNoNetWork() {
				// TODO Auto-generated method stub
				if(mListView.isRefreshing()) {
					mListView.getLoadingLayoutProxy().refreshFailed();
				}
				mFooterView.onRefreshFailed();
				mLoadingCover.onRefreshFailed(Define.MSG_NO_NETWORK);
				mPage--;
			}
			
			@Override
			public void onNoMoreData() {
				// TODO Auto-generated method stub
				mLoadingCover.onRefreshFailed(Define.MSG_NOTHING);
				mFooterView.onRefreshComplete();
				mListView.setOnScrollListener(null);
				if(mListView.isRefreshing()) {
					mListView.getLoadingLayoutProxy().refreshSuccess();
				}
			}
			
			@Override
			public void onResponseSuccess(
					List<ActivitiesLatestNoticeInfo> activitiesLatestNoticeInfos) {
				// TODO Auto-generated method stub
				if(this.requestCode == RequestCode.PULL) {
					mAdapter.clear();
				}
				mAdapter.addAll(activitiesLatestNoticeInfos);
				if (activitiesLatestNoticeInfos.size() < Define.LISTVIEW_ITEM_MIN_SIZE) {
					mFooterView.onRefreshComplete();
					mListView.setOnScrollListener(null);
					
				} else {
					mFooterView.onRefreshSuccess();
					mListView.setOnScrollListener(mFooterView);
				}
				
				mLoadingCover.onRefreshSuccess();
				if(mListView.isRefreshing()) {
					mListView.getLoadingLayoutProxy().refreshSuccess();
				}
			}
		};
	}

}
